package com.dataStructure.leetcode.队列;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 */
public class 用栈来实现队列 {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    /**
     * Initialize your data structure here.
     */
    public 用栈来实现队列() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    /**
     * Push element x to the back of queue.
     */
    //入队
    public void push(int x) {
        this.inStack.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    //出队
    public int pop() {
      /*  if (outStack.isEmpty()) {
            for (int i = 0; i < inStack.size(); i++) {
                outStack.push(inStack.pop());
            }
        }*/
        while (!outStack.isEmpty()){
                outStack.push(inStack.pop());
        }
        return outStack.pop();
    }

    /**
     * Get the front element.
     */
    //获取队头元素
    public int peek() {
      /*  if (outStack.isEmpty()) {
            for (int i = 0; i < inStack.size(); i++) {
                outStack.push(inStack.pop());
            }
        }*/
        while (!outStack.isEmpty()){
                outStack.push(inStack.pop());
        }
        return outStack.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    //是否为空
    public boolean empty() {
        return outStack.isEmpty()&&inStack.isEmpty();
    }
}
